// AUTHORS: Yiwei (Alan) Han, Kevin Lam 

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("IE5/6 not supported");
	}
}

var searchReq = getXmlHttpRequestObject();

function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('searchfield').value);
		searchReq.open("GET", 'ajax/suggest?searchQuery=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:search(this.innerHTML);" ';
					
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}

function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

function search(value) {
	var search = value.replace(/<i>.*<\/i>/gi,'');
	search = search.replace(/<(\/?)b>/gi,'');
	window.location='/search?q='+search;
}

var search_req = getXmlHttpRequestObject();

function getSearchContent(dept,course,section) {
	if (search_req.readyState == 4 ||search_req.readyState == 0) {
		search_req.open("GET", 'ajax/search?dept='+dept+'&course='+course+'&section='+section, true);
		search_req.onreadystatechange = handleSearchContent;
		search_req.send(null);
	}
}

function handleSearchContent() {
	if (search_req.readyState == 4) {
		var xmlDoc = search_req.responseXML.documentElement;
		var results = document.getElementById('results');
		var type = xmlDoc.getElementsByTagName("type")[0].childNodes[0].nodeValue;
		if(type == 'none') {
			results.innerHTML += "No results found";
		}
		else if (type == 'department'){  
			var xs = xmlDoc.getElementsByTagName("item");
			for (i = 0; i < xs.length; i++) {
				var dept = xs[i].getElementsByTagName("dept")[0].childNodes[0].nodeValue;
				var course = xs[i].getElementsByTagName("course")[0].childNodes[0].nodeValue;
				var title = xs[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
				var credits = xs[i].getElementsByTagName("credits")[0].childNodes[0].nodeValue;
				var prereq = 'None';
				if(xs[i].getElementsByTagName("prereq")[0].hasChildNodes())
					prereq = xs[i].getElementsByTagName("prereq")[0].childNodes[0].nodeValue;
				var coreq = 'None';
				if(xs[i].getElementsByTagName("coreq")[0].hasChildNodes())
					coreq = xs[i].getElementsByTagName("coreq")[0].childNodes[0].nodeValue;
				var html = '<div class="result-item" onmouseover="expand(\'#result-expand-'+i+'\');" onmouseout="collapse(\'#result-expand-'+i+'\');" onclick=\'window.location="/search?q='+dept+'%20'+course+'";\'>';
//				var html = '<div class="result-item">';
//				html += '<div class="trigger"><p class="result-header"><a href="/search?q='+dept+'%20'+course+'"><b>'+dept+'&nbsp;'+course+'</b></a></p>';
				html += '<div class="trigger" ><p class="result-header"><b>'+dept+'&nbsp;'+course+'</b>';
				html += '<p><i>'+title+'</i></p></div>';
				html += '<div class="expand" id="result-expand-'+i+'"><p>Credits: '+credits+'</p>';
				if(prereq != 'None')
					html += '<p>Pre-requisites: '+prereq+'</p>';
				if(coreq != 'None')
					html += '<p>Co-requisites: '+coreq+'</p></div>';
				html += '</div>';
//				$(".expand").hide();
//				
//				$(".result-item").click(function() {
//					$(".expand").slideToggle(1000);
//				});
				results.innerHTML += html;
			}
		}
		else if (type == 'course'){
			var xs = xmlDoc.getElementsByTagName("item");
			for (i = 0; i < xs.length; i++) {
				var dept = xs[i].getElementsByTagName("dept")[0].childNodes[0].nodeValue;
				var course = xs[i].getElementsByTagName("course")[0].childNodes[0].nodeValue;
				var section = xs[i].getElementsByTagName("section")[0].childNodes[0].nodeValue;
				var activity = xs[i].getElementsByTagName("activity")[0].childNodes[0].nodeValue;
				var term = xs[i].getElementsByTagName("term")[0].childNodes[0].nodeValue;
				var day = xs[i].getElementsByTagName("day")[0].childNodes[0].nodeValue;
				var time = xs[i].getElementsByTagName("time")[0].childNodes[0].nodeValue;
				var instructor = xs[i].getElementsByTagName("instructor")[0].childNodes[0].nodeValue;
				var html = '<div class="result-item" onclick=\'window.location="/detail?type=section&did='+dept+'&cid='+course+'&sid='+section+'";\'>';
				html += '<p class="result-header">'+dept+'&nbsp;'+course+'&nbsp;'+section+'</p>';
				html += '<p>Activity: '+activity+'</i></p>';
				html += '<p>Term: '+term+'</p>';
				html += '<p>Day: '+day+'</p>';
				html += '<p>Time: '+time+'</p>';
				html += '<p>Instructor: '+instructor+'</p>';
				html += '</div>';
				results.innerHTML += html;
			}
		}
		else if (type == 'section'){
			var xs = xmlDoc.getElementsByTagName("item");
			var dept = xs[0].getElementsByTagName("dept")[0].childNodes[0].nodeValue;
			var course = xs[0].getElementsByTagName("course")[0].childNodes[0].nodeValue;
			var section = xs[0].getElementsByTagName("section")[0].childNodes[0].nodeValue;
			window.location='/detail?type=section&did='+dept+'&cid='+course+'&sid='+section;
		}

	}
}

function expand(expand){
	//$(expand).slideDown(500); annoying as shit
}

function collapse(collapse){
	//$(collapse).slideUp(500);
}

function sleep(ms) {
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
}